import { defineConfig } from 'vite';
import { resolve } from 'path';
import dts from 'vite-plugin-dts';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default defineConfig({
  plugins: [
    dts({
      insertTypesEntry: true,
      rollupTypes: true,
    }),
  ],
  build: {
    lib: {
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'GenwaveJSCLI',
      formats: ['es', 'cjs'],
      fileName: (format) => `index.${format === 'es' ? 'js' : 'cjs'}`,
    },
    rollupOptions: {
      external: [
        'commander',
        'chalk',
        'ora',
        'inquirer',
        'axios',
        'fs-extra',
        'cosmiconfig',
        'simple-git',
        'semver',
        'node:fs',
        'node:path',
        'node:process',
        'node:url',
        'node:child_process',
        'node:os',
        /^node:/,
      ],
      output: {
        globals: {
          commander: 'Commander',
          chalk: 'Chalk',
          ora: 'Ora',
          inquirer: 'Inquirer',
          axios: 'Axios',
          'fs-extra': 'FsExtra',
          cosmiconfig: 'Cosmiconfig',
          'simple-git': 'SimpleGit',
          semver: 'Semver',
        },
      },
    },
    target: 'node16',
    minify: false,
    sourcemap: true,
    ssr: true,
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '@/core': resolve(__dirname, 'src/core'),
      '@/utils': resolve(__dirname, 'src/utils'),
      '@/types': resolve(__dirname, 'src/types'),
      '@/commands': resolve(__dirname, 'src/commands'),
    },
  },
});
